package com.yutian.endecryption.mock.service;

import com.yutian.endecryption.aes.util.SymmetricEncoder;
import com.yutian.endecryption.config.Config;
import com.yutian.endecryption.mock.mapper.ExaminerRecordMapper;
import com.yutian.endecryption.mock.pojo.ExaminerRecord;

/**
 * 考官备案信息 service
 *
 * @author zhang peng yang ming
 * @create 2017-08-02 10:32
 */
public class ExaminerRecordService extends BaseService<ExaminerRecordMapper> {

    /**
     * 根据身证号码查询考官备案信息
     *
     * @param sfzmhm 考官备案时的身份证明号码
     * @return
     */
    public ExaminerRecord retrieveRecord (String sfzmhm) {
        ExaminerRecord record = mapper(ExaminerRecordMapper.class).
                retrieve(SymmetricEncoder.AESEncode(Config.KEY, sfzmhm));
        sessionClose();

        if (record != null) {
            record.setSfzmhm(SymmetricEncoder.AESDecode(Config.KEY, record.getSfzmhm()));
            record.setXh(SymmetricEncoder.AESDecode(Config.KEY, record.getXh()));
            record.setXm(SymmetricEncoder.AESDecode(Config.KEY, record.getXm()));
            record.setJbr(SymmetricEncoder.AESDecode(Config.KEY, record.getJbr()));
        }
        return record;
    }

    /**
     * 根据身证号码更新考官备案信息
     *
     * @param record
     * @return
     */
    public int updateRecord (ExaminerRecord record) {
        record.setSfzmhm(SymmetricEncoder.AESEncode(Config.KEY, record.getSfzmhm()));
        record.setXh(SymmetricEncoder.AESEncode(Config.KEY, record.getXh()));
        record.setXm(SymmetricEncoder.AESEncode(Config.KEY, record.getXm()));
        record.setJbr(SymmetricEncoder.AESEncode(Config.KEY, record.getJbr()));
        int r = mapper(ExaminerRecordMapper.class).update(record);
        sessionClose();

        return r;
    }

    /**
     * 创建考官备案信息
     * @param record
     * @return
     */
    public int createRecord (ExaminerRecord record) {
        int r = 0;
        if (retrieveRecord(record.getSfzmhm()) != null) {
            r = updateRecord(record);
        } else {
            record.setSfzmhm(SymmetricEncoder.AESEncode(Config.KEY, record.getSfzmhm()));
            record.setXh(SymmetricEncoder.AESEncode(Config.KEY, record.getXh()));
            record.setXm(SymmetricEncoder.AESEncode(Config.KEY, record.getXm()));
            record.setJbr(SymmetricEncoder.AESEncode(Config.KEY, record.getJbr()));
            r = mapper(ExaminerRecordMapper.class).create(record);

            sessionClose();
        }

        return r;
    }
}
